<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class ModifypasswdForm extends CFormModel
{
	public $password;	public $passwordConfirm;	public $urlValid;	public $resetPassSuccess;	private $_userName;	public function init(){
						$key = Yii::app()->request->getParam("key");
		$time = Yii::app()->request->getParam("time");
		$this->_userName = Yii::app()->request->getParam("name");
		$this->urlValid = true;
		if(CSecurity::validateForgetpasswdUrl($key, $this->_userName, $time)==false){			$this->urlValid = false;
		}else{
			$user = User::model()->find("user_name=?",array($this->_userName));
			if(!isset($user)){				$this->urlValid = false;
			}else{				$this->urlValid = UserMeta::model()->count("meta_key='password_url_used' and meta_value='0' and user_id=?",array($user["id"]))>0?true:false;
			}
		}
		if($this->urlValid==false){
			$this->addError("password", "抱歉，该 key 似乎无效。");
		}
		$this->resetPassSuccess = false;
	}
	public function rules()
	{
		return array(
		array('password,passwordConfirm', 'required'),
		array('password', 'length', 'min'=>5, 'max'=>20),
		array('passwordConfirm', 'compare', 'compareAttribute' => 'password'),
		);
	}
	public function attributeLabels()
	{
		return array(
            'password'=>'新密码', 
		    'passwordConfirm'=>'确认新密码', 
		);
	}

	public function save(){
		if($this->validate() && $this->urlValid){
						$user = User::model()->find("user_name=?",array($this->_userName));
			if(isset($user)){
			    $salt = $user["salt"];
				$user["user_pass"]=CSecurity::signPasswd($this->password, $salt);
				$this->resetPassSuccess = true;
				$user->save();
			}
						$meta = UserMeta::model()->find("user_id=? and meta_key='password_url_used'",array($user["id"]));
			if(isset($meta)){
				$meta["meta_value"]="1";
				$meta->save();
			}
		}
	}

}
